<?php

class Db_Tables2_BContractRejection
{
	var $contract_id;
	var $reason;
	
	function __construct() {
		$this->dealers_db = Zend_Registry::get("dealers_db");
	}
	
	function AddRecord()
	{
		$result = FALSE;
		
		if($this->contract_id && $_SESSION['identity']['userid'])
		{
			$set = array(
				"contract_id" => $this->contract_id,
				"reason" => $this->reason,
				"emp_id" => $_SESSION['identity']['userid'],
				"emp_name" => $_SESSION['identity']['realname'],
				"ctime" => date("Y-m-d H:i:s")
			);
			
			$this->dealers_db->insert("b_contract_rejection", $set);
			
			$set = array(
				"status" => 0,
				"allow_edit" => 1
			);
			
			$where = "contract_id = ".$this->contract_id;
			
			$this->dealers_db->update("b_contract", $set, $where);
			
			$result = TRUE;
		}
		
		return $result;
	}
	
	function GetRecord()
	{
		$result = array(
			"result" => 0,
			"reason" => "",
			"emp_id" => 0,
			"emp_name" => "",
			"ctime" => ""
		);
		
		if($this->contract_id)
		{
			$select = $this->dealers_db->select();
			$select->from("b_contract_rejection", "*");
			$select->where("contract_id = ?", $this->contract_id);
			$select->order("ctime DESC");
			
			$row = $this->dealers_db->fetchRow($select);
			
			if($row)
			{
				$result = array(
					"result" => 1,
					"reason" => $row['reason'],
					"emp_id" => $row['emp_id'],
					"emp_name" => $row['emp_name'],
					"ctime" => $row['ctime']
				);
			}
		}
		
		return $result;
	}
}